'use strict';

import { ajax, nameSpace,generateMixed} from 'utils/index';

let ns = nameSpace('Index');
export const GET_LIST_SUCCESS = ns('GET_LIST_SUCCESS');
export const INIT_LIST =ns('INIT_LIST');
export const DELETE_ITEM =ns('DELETE_ITEM');
export const ADD_ITEM =ns('ADD_ITEM');
export const MODIFY_ITEM =ns('MODIFY_ITEM');
export const TOGGLE_ICON =ns('TOGGLE_ICON');
export const MOVE_ITEM =ns('MOVE_ITEM');
export const MOVING =ns('MOVING');
export const OVERING =ns('OVERING');
export const INIT_FORM =ns('INIT_FORM');
export const SET_PAGE_ID =ns('SET_PAGE_ID');
export const LOAD_START =ns('LOAD_START');
export const LOAD_ERR =ns('LOAD_ERR');
export const CLOSE_DIALOG =ns('CLOSE_DIALOG');

export function setPageId(pageId){
  return {type:SET_PAGE_ID,pageId};
}
export function initList(){
  return {type:INIT_LIST};
}
export function deleteItem(id,compId,pageId){
	if(!compId){
		return {type:DELETE_ITEM,id};
	}else{
		return (dispatch) => {
			//发送请求前
			dispatch({
			  type: LOAD_START
			});
			ajax({
			  api: 'deletePageComp',
			  method: 'GET',
			  data:{comp_id:compId,page_id:pageId}
			},(json) => {
				  //成功后
				dispatch({type:DELETE_ITEM,id});
			},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				  });
			});
		}
	}
}
export function addItem(obj,pageId,newComp){
	if(newComp=="new_comp"){
		//数据库操作
		return (dispatch) => {
			//发送请求前
			dispatch({
			  type: LOAD_START
			});
			
			let mix = "_"+generateMixed(6);
			for(var o in obj){
				let mix_val = {};
				if(o=="formData"){
					for(var v in obj[o]){
						mix_val[v+mix]=obj[o][v];
					}
					obj.formData = mix_val;
				}
				if(o=="formField"){
					for(var v in obj[o]){
						obj[o][v].name = v+mix;
						mix_val[v+mix]=obj[o][v];
					}
					obj.formField = mix_val;
				}
			}
			obj.mix = mix;
			obj.ordinal = obj.dataSource[obj.dataSource[0].atIndex].ordinal-1;

			ajax({
			  api: 'addPageComp',
			  method: 'GET',
			  data:{comp_code:obj.code,
					page_id:pageId,
					ordinal:obj.ordinal,
					mix}
			},(json) => {
				  //成功后
				obj.id = json.comp_id;
				dispatch({type:ADD_ITEM,obj});
			},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
				  });
			});
		}
	}else{
		//打开页面时的加载操作
		return {type:ADD_ITEM,obj}
	}
}
export function modifyItem(id,valueObj){
 return {type:MODIFY_ITEM,id,valueObj};
}
export function saveItem(id,ordinal,valueObj,pageId){
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'savePageCompData',
		  method: 'POST',
		  data:{ordinal:ordinal,
				data:valueObj,
				page_id:pageId}
		},(json) => {
			  //成功后
			dispatch({type:MODIFY_ITEM,id,valueObj});
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			  });
		});
	}
}
export function toggleIcon(id,obj){
  return {type:TOGGLE_ICON,id,obj}
}
export function moveItem(cardId,atIndex){
  return {type:MOVE_ITEM,cardId,atIndex}
}
export function moving(id){
  return {type:MOVING,id}
}
export function overing(atIndex){
  return {type:OVERING,atIndex}
}

export function initForm(form){
  return {type:INIT_FORM,form}
}
export function closeSystemDialog() {
	return {
    type: CLOSE_DIALOG
  };
}
